﻿/*
 * (c) Stefan Bojarovski 2010
 * */
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASTBuilder
{
    class FunctionNode : NodesCollection, ITreeNode
    {
        private LexemeType lexType = LexemeType.FUN_DEF;
        private ITreeNode Argument; //Identifier

        public FunctionNode(ITreeNode someId)
        {
            Argument = someId;
        }
        public LexemeType getLexemeType()
        {
            return lexType;
        }
        public void printNode(String parentLevel, StreamWriter someStream)
        {
            someStream.WriteLine(parentLevel + " " + "Function: ");
            Argument.printNode(parentLevel, someStream);
        }
        public List<Command> evalNode(ref Direction next,
                                        ref Dictionary<String, Pair<IdentifierType, object>> Environment)
        {
            List<Command> funResult = new List<Command>();
            IdentifierNode funNameNode = (IdentifierNode)Argument;
            String funName = funNameNode.getName();
            Command result;
            if (next != Direction.JUMP)
            {
                if (!Environment.ContainsKey(funName))
                {
                    Environment.Add(funName, new Pair<IdentifierType, object>(IdentifierType.FUNCTION, null));
                    result = new Command(CommandType.ENVIRONMENTMODIFICATION, funName, null);
                }
                else
                {
                    if (Environment[funName].getLeft() != IdentifierType.FUNCTION)
                    {
                        result = new Command(CommandType.ERRORUNCOMPATIBLETYPES, funName, null);
                    }
                    else
                    {
                        result = new Command(CommandType.ERRORALREADYDECLARED, funName, null);
                    }
                }

                funResult.Add(result);
                return funResult;
            }
            else
            {
                funResult.Add(new Command(CommandType.VOID, null, null));
                return funResult;
            }
        }
    }
}
